import { defineEventHandler, createError } from 'h3';
import { getDbConnection } from '../../utils/db.js';
import { requireUser } from '../../utils/auth.js';

export default defineEventHandler(async (event) => {
  try {
    const { userId } = requireUser(event);

    const db = await getDbConnection();
    const data = await db.all(
      `SELECT id, name, content, icon, iconColor, parent_id, created_at, updated_at 
       FROM notes 
       WHERE user_id = ?`,
      [userId]
    );

    return data;
  } catch (error) {
    console.error('Error fetching notes:', error);
    throw createError({ statusCode: 500, statusMessage: 'Failed to fetch notes' });
  }
});
